<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\User;
use App\Models\Configs;
use App\Models\UserInfos;
use App\Models\Users;
use App\Exports\UserExport;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;
use PhpParser\Node\Stmt\Switch_;

class UserController extends Controller
{

    /**
     * 页面
     * @return view
     */
    public function index()
    {
        return view('admin.user.index');
    }

    /**
     * 列表数据
     * @param Request $request
     * @return bool
     */
    public function list(Request $request)
    {
        try {
            $params = [
                'kwd' => (string)$request->get('kwd', ''),
                'type' => (int)$request->get('type', 0),
                'status' => (int)$request->get('status', 0),
            ];
            $lists = Users::getList($params);
            return $this->writeJson(0, $lists);
        } catch (\Exception $e) {
            return $this->writeJson(1, null, $e->getMessage());
        }
    }

    /**
     * 编辑
     * @param Request $request
     * @return bool|view
     */
    public function edit(Request $request)
    {
        try {
            $id = (int)$request->get('id', 0);
            if (empty($id)) {
                return $this->writeJson(1, null, '参数错误');
            }
            $info = Users::findOrFail($id);
            $info['sex'] = $info['sex'] == 1 ? '男' : '女';
            if($info['type'] == 2){
            	$info['type_name'] = "团长";
            }elseif ($info['type'] == 3){
            	$info['type_name'] = "悬赏主";
            }else{
            	$info['type_name'] = "赏金猎人";
            }
            
          //  $info['user_info'] = UserInfos::getOneList(['user_id'=>$id]);
            $info = json_encode($info);
            return view('admin.user.detail', compact('info'));
        } catch (\Exception $e) {
            return $this->writeJson(1, null, $e->getMessage());
        }
    }

    /**
     * 更新
     * @param User $request
     * @return bool
     */
    public function update(User $request)
    {
        try {
            $data = $request->all();
            $map = [
                'status' => $data['status'],
            ];
            
            if ($data['id']) {
                $info = Users::findOrFail($data['id']);
                $info->update($map);
                return $this->writeJson(0, null, '修改用户信息成功');
            } else {
                unset($data['id']);
                
                
                $base = Configs::getConfig('base');
                $data['avatar'] = $base['default_avatar'];
                Users::create($data);
                return $this->writeJson(0, null, '新增用户信息成功');
            }
        } catch (\Exception $e) {
            return $this->writeJson(1, null, $e->getMessage());
        }
    }

    /**
     * 拒绝
     * @param Request $request
     * @return bool
     */
    public function lock(Request $request)
    {
        try {
            $id = (int)$request->get('id', 0);
            if (empty($id)) {
                return $this->writeJson(1, null, '参数错误');
            }
            $info = Users::findOrFail($id);
            $info->update(['status' => Users::STATUS_3]);
            return $this->writeJson(0);
        } catch (\Exception $e) {
            return $this->writeJson(1, null, $e->getMessage());
        }
    }

    /**
     * 激活
     * @param Request $request
     * @return bool
     */
    public function active(Request $request)
    {
        try {
            $id = (int)$request->get('id', 0);
            if (empty($id)) {
                return $this->writeJson(1, null, '参数错误');
            }
            $info = Users::findOrFail($id);
            $info->update(['status' => Users::STATUS_2]);
            return $this->writeJson(0);
        } catch (\Exception $e) {
            return $this->writeJson(1, null, $e->getMessage());
        }
    }

   
    /**
     *  单个/多个删除
     * @param Request $request
     * @return bool
     */
    public function destroy(Request $request)
    {
        try {
            $id = $request->get('id');
            if (empty($id)) {
                return $this->writeJson(1, null, '参数错误');
            }
            if(is_array($id)){
                $where['id'] = ['in',$id];
                for ($i=0; $i<count($request['id']); $i++) {
                	$res =Users::where('id', $request['id'][$i])->update(['status' => 4]);   // 遍历删除
                }
            }else{
                $where['id'] = $id;
                $data['status'] = 4;
                Users::where($where)->update($data);
            }
            //修改软删除 19-7-31
            
            return $this->writeJson(0);
        } catch (\Exception $e) {
            return $this->writeJson(1, null, $e->getMessage());
        }
    }

    /**
     *  Excel导出
     * @param Request $request
     * @return bool
     */
    public function exports(Request $request)
    {
        $ids = $request->get('ids');
        $type = $request->get('type');
        $export = new UserExport($ids,$type);
        //这里可以传参到服务层
        return Excel::download($export, '用户表.xlsx');
    }

}
